package cn.gzjp.modules.logis.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;

import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.gzjp.common.constant.LogisUserType;
import cn.gzjp.common.utils.FlowIdUtil;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.modules.gas.entity.GasRecharge;
import cn.gzjp.modules.gas.service.AliyunSms;
import cn.gzjp.modules.gas.service.GasRechargeService;
import cn.gzjp.modules.logis.entity.*;
import cn.gzjp.modules.sys.entity.Log;
import cn.gzjp.modules.wx.service.WxService;
import cn.hutool.core.util.StrUtil;
import com.aliyuncs.exceptions.ClientException;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.modules.logis.dao.LogisDeliveryDao;

/**
 * 物流发货Service
 *
 * @author zengcp
 * @version 2018-03-22
 */
@Service
@Transactional(readOnly = true)
public class LogisDeliveryService extends CrudService<LogisDeliveryDao, LogisDelivery> {
    @Autowired
    private LogisGoodsPicService logisGoodsPicService;
    @Autowired
    private LogisDeliveryLogService logisDeliveryLogService;
    @Autowired
    private LogisUserService logisUserService;
    @Autowired
    private LogisUserUnionService logisUserUnionService;
    @Autowired
    private LogisCarService logisCarService;
    @Autowired
    private GasRechargeService gasRechargeService;
    @Autowired
    private WxService wxService;
    @Autowired
    private LogisBxDiscountService logisBxDiscountService;

    @Autowired
    private LogisInsurePhotoService logisInsurePhotoService;



    public LogisDelivery get(String id) {
        LogisDelivery delivery = super.get(id);
        LogisGoodsPic searchPic = new LogisGoodsPic();
        searchPic.setDeliveryId(id);
        delivery.setGoodsPicList(logisGoodsPicService.findList(searchPic));
        LogisDeliveryLog searchLog = new LogisDeliveryLog();
        searchLog.setDeliveryId(id);
        delivery.setDeliveryLogs(logisDeliveryLogService.findList(searchLog));
        return delivery;
    }

    public List<LogisDelivery> findList(LogisDelivery logisDelivery) {
        return super.findList(logisDelivery);
    }

    /**
     * 司机未完成的运输单
     *
     * @param openid
     * @return
     */
    public List<LogisDelivery> getMyNoOverCarriers(String openid) {
        LogisDelivery search = new LogisDelivery();
        search.setCarrier(openid);
        LogisUser user = logisUserService.get(openid);
        if (user != null && StrUtil.isNotBlank(user.getUnionId())) {
            search.setCarrier(user.getUnionId());
        }
        search.setDirverCurrent(true);
        return super.findList(search);
    }

    public Page<LogisDelivery> findPage(Page<LogisDelivery> page, LogisDelivery logisDelivery) {
        return super.findPage(page, logisDelivery);
    }

    @Transactional(readOnly = false)
    public void save(LogisDelivery logisDelivery) {
        LogisDelivery m = get(logisDelivery.getId());
        if (m != null) {
            super.save(logisDelivery);
        } else {
            logisDelivery.setProfit(new BigDecimal("0"));
            super.insert(logisDelivery, logisDelivery.getId());
        }

        if (StringUtils.isNotBlank(logisDelivery.getGoodsWeight()) && logisDelivery.getDriverUnitPrice() != null && logisDelivery.getOwnerUnitPrice() != null) {
            logisDelivery.setDriverFreight(logisDelivery.getDriverUnitPrice().multiply(new BigDecimal(logisDelivery.getGoodsWeight())));
            logisDelivery.setOwnerFreight(logisDelivery.getOwnerUnitPrice().multiply(new BigDecimal(logisDelivery.getGoodsWeight())));
            logisDelivery.setTax(logisDelivery.getOwnerFreight().divide(new BigDecimal(100)).multiply(new BigDecimal(logisDelivery.getTaxPoiint())));
            logisDelivery.setFreight(logisDelivery.getOwnerFreight().add(logisDelivery.getTax()).add(logisDelivery.getInsuranceFee()));// 总运费(包括货主看到的运费+提成+保险+发票税点)
            logisDelivery.setDriverReward(new BigDecimal(10));//司机上级奖励金 固定10元
            BigDecimal ticheng = logisDelivery.getOwnerFreight().subtract(logisDelivery.getDriverFreight());//提成(能支配的钱)
            BigDecimal reward = ticheng.divide(new BigDecimal(100)).multiply(new BigDecimal(25));//  下单奖励金总数 提成的25%  (发货人获得奖励金80%,推荐人获得奖励金的20%)
            BigDecimal commission = ticheng.divide(new BigDecimal(100)).multiply(new BigDecimal(60));//合伙人佣金（提成的60%）
            BigDecimal estimateProfit = ticheng.divide(new BigDecimal(100)).multiply(new BigDecimal(15)).subtract(logisDelivery.getDriverReward());//利润(订单未完成前是预估最低利润，完成后变成实际利润  提成的 15%-driverReward)
            logisDelivery.setTicheng(ticheng);
            //logisDelivery.setReward(reward);
            logisDelivery.setCommission(commission);
            logisDelivery.setEstimateProfit(estimateProfit);
            super.save(logisDelivery);
        } else {
            logisDelivery.setDriverFreight(null);
            logisDelivery.setOwnerFreight(null);
            logisDelivery.setTax(null);
            logisDelivery.setFreight(null);// 总运费(包括货主看到的运费+提成+保险+发票税点)
            logisDelivery.setDriverReward(new BigDecimal(10));//司机上级奖励金 固定10元
            logisDelivery.setTicheng(null);
            //logisDelivery.setReward(null);
            logisDelivery.setCommission(null);
            logisDelivery.setEstimateProfit(null);
            super.save(logisDelivery);
        }


    }

    @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
    public void save(LogisDelivery logisDelivery, String[] picPaths) {
        if (picPaths != null && ArrayUtils.isNotEmpty(picPaths)) {
            for (LogisGoodsPic pic : logisDelivery.getGoodsPicList()) {
                logisGoodsPicService.delete(pic);
            }
            for (String filePath : picPaths) {
                LogisGoodsPic goodsPic = new LogisGoodsPic();
                goodsPic.setDeliveryId(logisDelivery.getId());
                goodsPic.setPath(filePath);
                logisGoodsPicService.save(goodsPic);

            }
        }
        super.save(logisDelivery);
    }

    @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
    public LogisDelivery driverUploadPics(LogisDelivery delivery, String[] picPaths) {
//        if (StringUtils.isNotBlank(delivery.getGoodsWeight())) {
//            delivery.setDriverFreight(delivery.getDriverUnitPrice().multiply(new BigDecimal(delivery.getGoodsWeight())));
//            delivery.setOwnerFreight(delivery.getOwnerUnitPrice().multiply(new BigDecimal(delivery.getGoodsWeight())));
//            delivery.setTax(delivery.getOwnerFreight().divide(new BigDecimal(100)).multiply(new BigDecimal(delivery.getTaxPoiint())));
//            delivery.setFreight(delivery.getOwnerFreight().add(delivery.getTax()).add(delivery.getInsuranceFee()));
//            delivery.setDriverReward(new BigDecimal(10));
//
//            BigDecimal ticheng = delivery.getOwnerFreight().subtract(delivery.getDriverFreight());//提成(能支配的钱)
//
//            BigDecimal reward = ticheng.divide(new BigDecimal(100)).multiply(new BigDecimal(25));//  下单奖励金总数 提成的25%  (发货人获得奖励金80%,推荐人获得奖励金的20%)
//            BigDecimal commission = ticheng.divide(new BigDecimal(100)).multiply(new BigDecimal(60));//合伙人佣金（提成的60%）
//            BigDecimal profit = ticheng.subtract(reward).subtract(commission).subtract(delivery.getDriverReward());//利润(订单未完成前是预估最低利润，完成后变成实际利润  ticheng-reward-commission-driverReward)
//            delivery.setTicheng(ticheng);
//            delivery.setReward(reward);
//            delivery.setCommission(commission);//合伙人佣金
//            delivery.setProfit(profit);
//
//        }
        if (picPaths != null && ArrayUtils.isNotEmpty(picPaths)) {
            for (String filePath : picPaths) {
                LogisGoodsPic goodsPic = new LogisGoodsPic();
                goodsPic.setDeliveryId(delivery.getId());
                goodsPic.setPath(filePath);
                logisGoodsPicService.save(goodsPic);

            }
        }
        save(delivery);
        return this.get(delivery.getId());
    }

    @Transactional(readOnly = false)
    public void delete(LogisDelivery logisDelivery) {
        super.deleteByLogic(logisDelivery);
    }

    @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
    public void addDelivery(LogisDelivery delivery, String[] picPaths, String openid) {
        LogisUserUnion user = logisUserUnionService.get(delivery.getOpenid());
        user.setJiaocheNum(user.getJiaocheNum()+1);
        logisUserUnionService.save(user);
        LogisBxDiscount bxDiscount =  logisBxDiscountService.get("1");

        delivery.setHwbx(bxDiscount.getBeValue());
        delivery.setHwbxText(bxDiscount.getName());
        delivery.setHwbxYhText(bxDiscount.getYhName());
        delivery.setInsuranceFee0(new BigDecimal(bxDiscount.getBfValue()));//优惠前
        delivery.setInsuranceFee(new BigDecimal(bxDiscount.getYhhbfValue()));//优化后

        this.insert(delivery, FlowIdUtil.getInstance().getFlowidNoYear() + "");
        if (picPaths != null && ArrayUtils.isNotEmpty(picPaths)) {
            for (String filePath : picPaths) {
                LogisGoodsPic goodsPic = new LogisGoodsPic();
                goodsPic.setDeliveryId(delivery.getId());
                goodsPic.setPath(filePath);
                logisGoodsPicService.save(goodsPic);

            }
        }
        LogisDeliveryLog deliveryLog = new LogisDeliveryLog();
        deliveryLog.setOpenid(openid);
        deliveryLog.setDeliveryId(delivery.getId());
        deliveryLog.setStatus(delivery.getStatus());
        deliveryLog.setType("1");
        logisDeliveryLogService.save(deliveryLog);
        LogisUser logisUser = logisUserService.get(openid);
        logisUser.setType("1");
        logisUser.setJiaocheNum(logisUser.getJiaocheNum() + 1);
        logisUserService.save(logisUser);

        LogisInsurePhoto insurePhoto = logisInsurePhotoService.get(delivery.getId());
        if (insurePhoto == null) {
            insurePhoto = new LogisInsurePhoto();
            insurePhoto.setId(delivery.getId());
            insurePhoto.setStatus1("0");
            insurePhoto.setStatus2("0");
            insurePhoto.setStatus3("0");
            insurePhoto.setStatus4("0");
            insurePhoto.setStatus5("0");
        }
        insurePhoto.setGoodsName(delivery.getGoodsName());
        insurePhoto.setStartAddress(delivery.getStartProvince() + delivery.getStartCity());
        insurePhoto.setEndAddress(delivery.getEndProvince() + delivery.getEndCity());
        insurePhoto.setDriverName(delivery.getCarrierName());
        insurePhoto.setLicensePlate(delivery.getLicensePlate());
        insurePhoto.setReceiverName(delivery.getReceiverName());
        insurePhoto.setReceiverMobile(delivery.getReceiverMobile());
        insurePhoto.setDeliveryId(delivery.getId());
        insurePhoto.setHwbx(delivery.getHwbx());
        insurePhoto.setHwbxText(delivery.getHwbxText());
        logisInsurePhotoService.save(insurePhoto);

//
//		try {
//			AliyunSms.sendMsg("百灵智慧物流",logisUser.getMobile(),"1");
//		} catch (ClientException e) {
//			e.printStackTrace();
//		}
    }

    public static Logger log = LoggerFactory.getLogger("womsLog");

//    @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
//    public void finish(LogisDelivery delivery) {
//        //运输已完成
//        delivery.setEstimateProfit(delivery.getProfit());//利润
//        GasRecharge recharge = new GasRecharge();//奖励金
//        BigDecimal value = new BigDecimal("0.5");
//        if (StrUtil.isNotBlank(delivery.getGoodsName())) {
//            value = value.add(new BigDecimal("0.1"));
//        }
//        if (StrUtil.isNotBlank(delivery.getWholeCar())) {
//            value = value.add(new BigDecimal("0.1"));
//        }
//        if (StrUtil.isNotBlank(delivery.getGoodsWeight()) || StrUtil.isNotBlank(delivery.getGoodsVolume())) {
//            value = value.add(new BigDecimal("0.1"));
//        }
//        //(发货人获得奖励金80%,推荐人获得奖励金的20%)
//        recharge.setGive(delivery.getReward().multiply(value));
//        recharge.setAmount(delivery.getReward().multiply(value));
//        recharge.setOpenid(delivery.getOpenid());
//        recharge.setStartCity(delivery.getStartCity());
//        recharge.setStartDistrict(delivery.getStartDistrict());
//        recharge.setEndCity(delivery.getEndCity());
//        recharge.setEndDistrict(delivery.getEndDistrict());
//        recharge.setContent("下单叫车奖励金");
//        recharge.setWxOrderId(delivery.getId());
//        gasRechargeService.save(recharge);
//        LogisUser user = logisUserService.get(delivery.getOpenid());
//        user.setBalance(user.getBalance().add(delivery.getReward().multiply(value)));
//        logisUserService.save(user);
//        //推荐人要也要20%
//        String shareOpenid = "admin";//默认推荐人为admin
//        if (StringUtils.isNotBlank(user.getShareOpenid())) {
//            shareOpenid = user.getShareOpenid();
//        }
//        recharge = new GasRecharge();
//        recharge.setGive(delivery.getReward().multiply(new BigDecimal("0.2")));
//        recharge.setAmount(delivery.getReward().multiply(new BigDecimal("0.2")));
//        recharge.setOpenid(shareOpenid);
//        recharge.setStartCity(delivery.getStartCity());
//        recharge.setStartDistrict(delivery.getStartDistrict());
//        recharge.setEndCity(delivery.getEndCity());
//        recharge.setEndDistrict(delivery.getEndDistrict());
//        recharge.setContent("推广奖励金");
//        recharge.setWxOrderId(delivery.getId());
//        gasRechargeService.save(recharge);
//        user = logisUserService.get(user.getShareOpenid());
//        if (user != null && user.getBalance() != null && delivery.getReward() != null) {
//            user.setBalance(user.getBalance().add(delivery.getReward().multiply(new BigDecimal("0.2"))));
//            logisUserService.save(user);
//        }
//        //运输已完成司机得运费
//        LogisUser carrier = logisUserService.get(delivery.getCarrier());
//        carrier.setBalance(carrier.getBalance().add(delivery.getDriverFreight()));//当前余额
//        carrier.setIncomeTotal(carrier.getIncomeTotal().add(delivery.getDriverFreight()));//佣金总额
//        logisUserService.save(carrier);
//        recharge = new GasRecharge();
//        recharge.setGive(delivery.getDriverFreight());
//        recharge.setAmount(delivery.getDriverFreight());
//        recharge.setOpenid(delivery.getCarrier());
//        recharge.setStartCity(delivery.getStartCity());
//        recharge.setStartDistrict(delivery.getStartDistrict());
//        recharge.setEndCity(delivery.getEndCity());
//        recharge.setEndDistrict(delivery.getEndDistrict());
//        recharge.setContent("运费结算款");
//        recharge.setWxOrderId(delivery.getId());
//        gasRechargeService.save(recharge);
//        super.save(delivery);
//        //司机接单奖励上级
//        BigDecimal driverReward = delivery.getDriverReward();
//        String carrierShareOpenid = carrier.getShareOpenid();
//        if (StringUtils.isNotBlank(carrierShareOpenid)) {
//            LogisUser carrierShareUser = logisUserService.get(carrierShareOpenid);
//            carrierShareUser.setBalance(carrierShareUser.getBalance().add(driverReward));//余额总额
//            carrierShareUser.setIncomeTotal(carrierShareUser.getIncomeTotal().add(driverReward));//收入总额
//            logisUserService.save(carrierShareUser);
//            recharge = new GasRecharge();
//            recharge.setGive(driverReward);
//            recharge.setAmount(driverReward);
//            recharge.setOpenid(carrierShareOpenid);
//            recharge.setStartCity(delivery.getStartCity());
//            recharge.setStartDistrict(delivery.getStartDistrict());
//            recharge.setEndCity(delivery.getEndCity());
//            recharge.setEndDistrict(delivery.getEndDistrict());
//            recharge.setContent("推广奖励金");
//            recharge.setWxOrderId(delivery.getId());
//            gasRechargeService.save(recharge);
//        } else {//如果司机没有上级,这部分就归到利润里来
//            delivery.setDriverReward(new BigDecimal("0"));//无上级设置成0
//            delivery.setProfit(delivery.getProfit().add(driverReward));
//            this.save(delivery);
//        }
//        String partnerOpenid = carrier.getShareOpenid();
//        BigDecimal commission = delivery.getCommission();
//        if (StringUtils.isNotBlank(partnerOpenid)) {//合伙人
//            LogisUser partnerUser = logisUserService.get(partnerOpenid);
//            if (partnerUser != null && LogisUserType.PARTNER.equals(partnerUser.getType())) {//合伙人存在
//
//                carrier.setCommissionTotal(carrier.getCommissionTotal().add(commission));//当前车主给合伙人累积挣得的钱
//                logisUserService.save(carrier);
//
//                partnerUser.setBalance(partnerUser.getBalance().add(commission));
//                partnerUser.setIncomeTotal(partnerUser.getIncomeTotal().add(commission));//收入总额
//                partnerUser.setCommissionTotal(carrier.getCommissionTotal().add(commission));//车主给合伙人累积挣总额
//                logisUserService.save(partnerUser);
//
//                recharge = new GasRecharge();
//                recharge.setGive(driverReward);
//                recharge.setAmount(driverReward);
//                recharge.setOpenid(partnerOpenid);
//                recharge.setStartCity(delivery.getStartCity());
//                recharge.setStartDistrict(delivery.getStartDistrict());
//                recharge.setEndCity(delivery.getEndCity());
//                recharge.setEndDistrict(delivery.getEndDistrict());
//                recharge.setContent("合伙人佣金提成");
//                recharge.setWxOrderId(delivery.getId());
//                gasRechargeService.save(recharge);
//
//            } else {//如果没有合伙人，这部分就归到利润里来
//                delivery.setCommission(new BigDecimal("0"));
//                delivery.setProfit(delivery.getProfit().add(commission));
//                this.save(delivery);
//            }
//        } else {//如果没有合伙人，这部分就归到利润里来
//            delivery.setCommission(new BigDecimal("0"));
//            delivery.setProfit(delivery.getProfit().add(commission));
//            this.save(delivery);
//        }
//    }



    @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
    public void finishDelivery(LogisDelivery delivery) {
        if(delivery.getReward()==null){
            return;
        }
        //运输已完成

        BigDecimal dif = delivery.getReward();
        GasRecharge recharge = new GasRecharge();//奖励金
        recharge.setGive(delivery.getReward());
        recharge.setAmount(delivery.getReward());
        recharge.setOpenid(delivery.getDevOpenid());//工厂端本人
        recharge.setSupportOpenid(delivery.getDevOpenid());//自己提供的
        recharge.setStartCity(delivery.getStartCity());
        recharge.setStartDistrict(delivery.getStartDistrict());
        recharge.setEndCity(delivery.getEndCity());
        recharge.setEndDistrict(delivery.getEndDistrict());
        recharge.setContent("下单叫车奖励金");
        recharge.setWxOrderId(delivery.getId());
        recharge.setStartProvince(delivery.getStartProvince());
        recharge.setEndProvince(delivery.getEndProvince());
        recharge.setComeFrom("0");
        gasRechargeService.save(recharge);

        LogisUser user = logisUserService.get(delivery.getDevOpenid());
        if(user==null){
            return;
        }
        if(user.getBalance()==null){
            user.setBalance(new BigDecimal("0"));
        }
        user.setBalance(user.getBalance().add(delivery.getReward()));
        user.setIncomeTotal(user.getIncomeTotal().add(delivery.getReward()));//收入总额
        logisUserService.save(user);
        //推广人每单20元
        String shareOpenid = "admin";//默认推荐人为admin
        if (StringUtils.isNotBlank(user.getShareOpenid())) {
            shareOpenid = user.getShareOpenid();
        }
        recharge = new GasRecharge();
        recharge.setGive(new BigDecimal("20"));
        recharge.setAmount(new BigDecimal("20"));
        recharge.setOpenid(shareOpenid);
        recharge.setSupportOpenid(delivery.getDevOpenid());//下单人提供的
        recharge.setStartCity(delivery.getStartCity());
        recharge.setStartDistrict(delivery.getStartDistrict());
        recharge.setEndCity(delivery.getEndCity());
        recharge.setEndDistrict(delivery.getEndDistrict());
        recharge.setContent("推广奖励金");
        recharge.setWxOrderId(delivery.getId());
        recharge.setStartProvince(delivery.getStartProvince());
        recharge.setEndProvince(delivery.getEndProvince());
        recharge.setComeFrom("1");
        gasRechargeService.save(recharge);
        user = logisUserService.get(user.getShareOpenid());
        if (user != null && user.getBalance() != null) {
            user.setBalance(user.getBalance().add(recharge.getAmount()));
            user.setIncomeTotal(user.getIncomeTotal().add(recharge.getAmount()));//收入总额
            logisUserService.save(user);
        }
        dif = dif.add(recharge.getAmount());

        //司机首单奖励金——固定50元，只有完成第一单有，后面就没有了
        recharge = new GasRecharge();
        recharge.setGive(new BigDecimal("50"));//
        recharge.setAmount(new BigDecimal("50"));
        recharge.setOpenid(delivery.getCarOpenid());
        recharge.setSupportOpenid(delivery.getDevOpenid());//下单人提供的
        List<GasRecharge> gasRecharges = gasRechargeService.findList(recharge);
        if(gasRecharges!=null&&gasRecharges.size()<=0){
            recharge.setStartCity(delivery.getStartCity());
            recharge.setStartDistrict(delivery.getStartDistrict());
            recharge.setEndCity(delivery.getEndCity());
            recharge.setEndDistrict(delivery.getEndDistrict());
            recharge.setContent("司机首单奖励金");
            recharge.setWxOrderId(delivery.getId());
            recharge.setStartProvince(delivery.getStartProvince());
            recharge.setEndProvince(delivery.getEndProvince());
            recharge.setComeFrom("0");
            gasRechargeService.save(recharge);
            user = logisUserService.get(delivery.getCarOpenid());
            if (user != null && user.getBalance() != null) {
                user.setBalance(user.getBalance().add(recharge.getAmount()));
                user.setIncomeTotal(user.getIncomeTotal().add(recharge.getAmount()));//收入总额
                logisUserService.save(user);
            }
            dif = dif.add(recharge.getAmount());
        }


       //司机推广奖励金——给上级推广人每单奖励10元
        LogisUser carrier = logisUserService.get(delivery.getCarOpenid());
        String carrierShareOpenid = carrier.getShareOpenid();
        if (StringUtils.isNotBlank(carrierShareOpenid)) {
            LogisUser carrierShareUser = logisUserService.get(carrierShareOpenid);
            carrierShareUser.setBalance(carrierShareUser.getBalance().add(new BigDecimal("10")));//余额总额
            carrierShareUser.setIncomeTotal(carrierShareUser.getIncomeTotal().add(new BigDecimal("10")));//收入总额
            logisUserService.save(carrierShareUser);
            recharge = new GasRecharge();
            recharge.setGive(new BigDecimal("10"));
            recharge.setAmount(new BigDecimal("10"));
            recharge.setOpenid(carrierShareOpenid);
            recharge.setSupportOpenid(delivery.getDevOpenid());//下单人提供的
            recharge.setStartCity(delivery.getStartCity());
            recharge.setStartDistrict(delivery.getStartDistrict());
            recharge.setEndCity(delivery.getEndCity());
            recharge.setEndDistrict(delivery.getEndDistrict());
            recharge.setContent("司机推广奖励金");
            recharge.setWxOrderId(delivery.getId());
            recharge.setStartProvince(delivery.getStartProvince());
            recharge.setEndProvince(delivery.getEndProvince());
            recharge.setComeFrom("1");
            gasRechargeService.save(recharge);
            delivery.setDriverReward(new BigDecimal("10"));
            dif = dif.add(recharge.getAmount());//累积支出
        } else {//如果司机没有上级,这部分就归到利润里来
            delivery.setDriverReward(new BigDecimal("0"));//无上级设置成0
            this.save(delivery);
        }

        delivery.setProfit(delivery.getDeposit().subtract(dif));
        delivery.setCarrierFinishTime(new Date());
        this.save(delivery);

    }
}